package core

import (
	"global/libs/structs"
	"github.com/garyburd/redigo/redis"
	"fmt"
	"global/constants"
)

// 保存法币汇率信息
func SaveCnyRate(conn redis.Conn, datas map[string]structs.CnyRate) error {
	conn.Send("MULTI")
	for _, v := range datas {
		key := fmt.Sprintf(constants.CommonCnyRateKey, v.Coin)
		conn.Send("HMSET", redis.Args{}.Add(key).AddFlat(&v)...)
	}
	_, err := conn.Do("EXEC")
	return err
}

func GetCnyRateByCoin(conn redis.Conn, currency string) (string, error) {
	key := fmt.Sprintf(constants.CommonCnyRateKey, currency)
	s, err := redis.String(conn.Do("HGET", key, "rate"))
	if err != nil {
		return "", err
	}
	return s, nil
}